home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0192.ARJ / PICTBOX.C < prev    next >
Text File  |  1991-11-07  |  8KB  |  281 lines

  1. /* -------------- pictbox.c -------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. typedef struct    {
  6.     enum VectTypes vt;
  7.     RECT rc;
  8. } VECT;
  9.  
  10. unsigned char CharInWnd[] = "─│┌┐┘└┼├┤┴┬";
  11.  
  12. unsigned char VectCvt[3][11][2][4] = {
  13.     {   /* --- first character in collision vector --- */
  14.         /* ( drawing ─ ) ( drawing │ ) */
  15.              {{"───"},     {"┌├└"}},
  16.              {{"┌┬┐"},     {"│││"}},
  17.              {{"┌┬┬"},     {"┌├├"}},
  18.              {{"┐┐┐"},     {"┐┐┐"}},
  19.              {{"┘┘┘"},     {"┘┘┘"}},
  20.              {{"└┴┴"},     {"├├└"}},
  21.              {{"┼┼┼"},     {"┼┼┼"}},
  22.              {{"├┼┼"},     {"├├├"}},
  23.              {{"┤┤┤"},     {"┤┤┤"}},
  24.              {{"┴┴┴"},     {"┴┴┴"}},
  25.              {{"┬┬┬"},     {"┬┼┼"}}    },
  26.     {   /* --- middle character in collision vector --- */
  27.         /* ( drawing ─ ) ( drawing │ ) */
  28.              {{"───"},     {"┬┼┴"}},
  29.              {{"├┼┤"},     {"│││"}},
  30.              {{"┌┌┌"},     {"┌┌┌"}},
  31.              {{"┐┐┐"},     {"┐┐┐"}},
  32.              {{"┘┘┘"},     {"┘┘┘"}},
  33.              {{"└└└"},     {"└└└"}},
  34.              {{"┼┼┼"},     {"┼┼┼"}},
  35.              {{"├├├"},     {"├├├"}},
  36.              {{"┼┼┤"},     {"┤┤┤"}},
  37.              {{"┴┴┴"},     {"┼┼┴"}},
  38.              {{"┬┬┬"},     {"┬┬┬"}}    },
  39.     {   /* --- last character in collision vector --- */
  40.         /* ( drawing ─ ) ( drawing │ ) */
  41.              {{"───"},     {"┐┤┘"}},
  42.              {{"└┴┘"},     {"│││"}},
  43.              {{"┌┌┌"},     {"┌┌┌"}},
  44.              {{"┬┬┐"},     {"┐┤┤"}},
  45.              {{"┴┴┘"},     {"┤┤┘"}},
  46.              {{"└└└"},     {"└└└"}},
  47.              {{"┼┼┼"},     {"┼┼┼"}},
  48.              {{"├├├"},     {"├├├"}},
  49.              {{"┼┼┤"},     {"┤┤┤"}},
  50.              {{"┴┴┴"},     {"┼┼┴"}},
  51.              {{"┬┬┬"},     {"┬┬┬"}}    }
  52. };
  53.  
  54. /* -- compute whether character is first, middle, or last -- */
  55. static int FindVector(WINDOW wnd, RECT rc, int x, int y)
  56. {
  57.     RECT rcc;
  58.     VECT *vc = wnd->VectorList;
  59.     int i, coll = -1;
  60.     for (i = 0; i < wnd->VectorCount; i++)    {
  61.         if ((vc+i)->vt == VECTOR)    {
  62.             rcc = (vc+i)->rc;
  63.             /* --- skip the colliding vector --- */
  64.             if (rcc.lf == rc.lf && rcc.rt == rc.rt &&
  65.                     rcc.tp == rc.tp && rc.bt == rcc.bt)
  66.                 continue;
  67.             if (rcc.tp == rcc.bt)    {
  68.                 /* ---- horizontal vector,
  69.                     see if character is in it --- */
  70.                 if (rc.lf+x >= rcc.lf && rc.lf+x <= rcc.rt &&
  71.                         rc.tp+y == rcc.tp)    {
  72.                     /* --- it is --- */
  73.                     if (rc.lf+x == rcc.lf)
  74.                         coll = 0;
  75.                     else if (rc.lf+x == rcc.rt)
  76.                         coll = 2;
  77.                     else 
  78.                         coll = 1;
  79.                 }
  80.             }
  81.             else     {
  82.                 /* ---- vertical vector,
  83.                     see if character is in it --- */
  84.                 if (rc.tp+y >= rcc.tp && rc.tp+y <= rcc.bt &&
  85.                         rc.lf+x == rcc.lf)    {
  86.                     /* --- it is --- */
  87.                     if (rc.tp+y == rcc.tp)
  88.                         coll = 0;
  89.                     else if (rc.tp+y == rcc.bt)
  90.                         coll = 2;
  91.                     else 
  92.                         coll = 1;
  93.                 }
  94.             }
  95.         }
  96.     }
  97.     return coll;
  98. }
  99.  
  100. static void PaintVector(WINDOW wnd, RECT rc)
  101. {
  102.     int i, cw, fml, vertvect, coll, len;
  103.     unsigned int ch, nc;
  104.  
  105.     if (rc.rt == rc.lf)    {
  106.         /* ------ vertical vector ------- */
  107.         nc = '│';
  108.         vertvect = 1;
  109.         len = rc.bt-rc.tp+1;
  110.     }
  111.     else     {
  112.         /* ------ horizontal vector ------- */
  113.         nc = '─';
  114.         vertvect = 0;
  115.         len = rc.rt-rc.lf+1;
  116.     }
  117.  
  118.     for (i = 0; i < len; i++)    {
  119.         unsigned int newch = nc;
  120.         int xi = 0, yi = 0;
  121.         if (vertvect)
  122.             yi = i;
  123.         else
  124.             xi = i;
  125.         ch = videochar(GetClientLeft(wnd)+rc.lf+xi,
  126.                     GetClientTop(wnd)+rc.tp+yi);
  127.         for (cw = 0; cw < sizeof(CharInWnd); cw++)    {
  128.             if (ch == CharInWnd[cw])    {
  129.                 /* ---- hit another vector character ---- */
  130.                 if ((coll=FindVector(wnd, rc, xi, yi)) != -1) {
  131.                     /* compute first/middle/last subscript */
  132.                     if (i == len-1)
  133.                         fml = 2;
  134.                     else if (i == 0)
  135.                         fml = 0;
  136.                     else
  137.                         fml = 1;
  138.                     newch = VectCvt[coll][cw][vertvect][fml];
  139.                 }
  140.             }
  141.         }
  142.         PutWindowChar(wnd, newch, rc.lf+xi, rc.tp+yi);
  143.     }
  144. }
  145.  
  146. static void PaintBar(WINDOW wnd, RECT rc, enum VectTypes vt)
  147. {
  148.     int i, vertbar, len;
  149.     unsigned int tys[] = {'█', '▓', '▒', '░'};
  150.     unsigned int nc = tys[vt-1];
  151.  
  152.     if (rc.rt == rc.lf)    {
  153.         /* ------ vertical bar ------- */
  154.         vertbar = 1;
  155.         len = rc.bt-rc.tp+1;
  156.     }
  157.     else     {
  158.         /* ------ horizontal bar ------- */
  159.         vertbar = 0;
  160.         len = rc.rt-rc.lf+1;
  161.     }
  162.  
  163.     for (i = 0; i < len; i++)    {
  164.         int xi = 0, yi = 0;
  165.         if (vertbar)
  166.             yi = i;
  167.         else
  168.             xi = i;
  169.         PutWindowChar(wnd, nc, rc.lf+xi, rc.tp+yi);
  170.     }
  171. }
  172.  
  173. static void PaintMsg(WINDOW wnd)
  174. {
  175.     int i;
  176.     VECT *vc = wnd->VectorList;
  177.     for (i = 0; i < wnd->VectorCount; i++)    {
  178.         if (vc->vt == VECTOR)
  179.             PaintVector(wnd, vc->rc);
  180.         else
  181.             PaintBar(wnd, vc->rc, vc->vt);
  182.         vc++;
  183.     }
  184. }
  185.  
  186. static void DrawVectorMsg(WINDOW wnd,PARAM p1,enum VectTypes vt)
  187. {
  188.     if (p1)    {
  189.         wnd->VectorList = realloc(wnd->VectorList,
  190.                 sizeof(VECT) * (wnd->VectorCount + 1));
  191.         if (wnd->VectorList != NULL)    {
  192.             VECT vc;
  193.             vc.vt = vt;
  194.             vc.rc = *(RECT *)p1;
  195.             *(((VECT *)(wnd->VectorList))+wnd->VectorCount)=vc;
  196.             wnd->VectorCount++;
  197.         }
  198.     }
  199. }
  200.  
  201. static void DrawBoxMsg(WINDOW wnd, PARAM p1)
  202. {
  203.     if (p1)    {
  204.         RECT rc = *(RECT *)p1;
  205.         rc.bt = rc.tp;
  206.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, TRUE);
  207.         rc = *(RECT *)p1;
  208.         rc.lf = rc.rt;
  209.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, FALSE);
  210.         rc = *(RECT *)p1;
  211.         rc.tp = rc.bt;
  212.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, TRUE);
  213.         rc = *(RECT *)p1;
  214.         rc.rt = rc.lf;
  215.         SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, FALSE);
  216.     }
  217. }
  218.  
  219. int PictureProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  220. {
  221.     switch (msg)    {
  222.         case PAINT:
  223.             BaseWndProc(PICTUREBOX, wnd, msg, p1, p2);
  224.             PaintMsg(wnd);
  225.             return TRUE;
  226.         case DRAWVECTOR:
  227.             DrawVectorMsg(wnd, p1, VECTOR);
  228.             return TRUE;
  229.         case DRAWBOX:
  230.             DrawBoxMsg(wnd, p1);
  231.             return TRUE;
  232.         case DRAWBAR:
  233.             DrawVectorMsg(wnd, p1, p2);
  234.             return TRUE;
  235.         case CLOSE_WINDOW:
  236.             if (wnd->VectorList != NULL)
  237.                 free(wnd->VectorList);
  238.             break;
  239.         default:
  240.             break;
  241.     }
  242.     return BaseWndProc(PICTUREBOX, wnd, msg, p1, p2);
  243. }
  244.  
  245. static RECT PictureRect(int x, int y, int len, int hv)
  246. {
  247.     RECT rc;
  248.     rc.lf = rc.rt = x;
  249.     rc.tp = rc.bt = y;
  250.     if (hv)
  251.         /* ---- horizontal vector ---- */
  252.         rc.rt += len-1;
  253.     else
  254.         /* ---- vertical vector ---- */
  255.         rc.bt += len-1;
  256.     return rc;
  257. }
  258.  
  259. void DrawVector(WINDOW wnd, int x, int y, int len, int hv)
  260. {
  261.     RECT rc = PictureRect(x,y,len,hv);
  262.     SendMessage(wnd, DRAWVECTOR, (PARAM) &rc, 0);
  263. }
  264.  
  265. void DrawBox(WINDOW wnd, int x, int y, int ht, int wd)
  266. {
  267.     RECT rc;
  268.     rc.lf = x;
  269.     rc.tp = y;
  270.     rc.rt = x+wd-1;
  271.     rc.bt = y+ht-1;
  272.     SendMessage(wnd, DRAWBOX, (PARAM) &rc, 0);
  273. }
  274.  
  275. void DrawBar(WINDOW wnd,enum VectTypes vt,
  276.                         int x,int y,int len,int hv)
  277. {
  278.     RECT rc = PictureRect(x,y,len,hv);
  279.     SendMessage(wnd, DRAWBAR, (PARAM) &rc, (PARAM) vt);
  280. }
  281.